package sg.com.techstudio.tafep.excel;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import sg.com.techstudio.tafep.controller.BaseController;
import sg.com.techstudio.tafep.obj.ParticipantObj;
import sg.com.techstudio.tafep.obj.RegisterObj;

@Component("reportRegistrationExcel")
public class ReportRegistrationExcel extends BaseExcel {

	private static final Log log = LogFactory.getLog(ReportRegistrationExcel.class);
	
	@Override
	protected String getFilename() {
		return "registration_"+FILENAME_SUFFIX_SDF.format(Calendar.getInstance().getTime())+EXCEL_EXTENSION;
	}

	@Override
	protected String getTabname() {
		return getTabname(0);
	}
	
	protected String getTabname(int i) {
		switch(i) {
		case 0:
			return "registration";
		case 1:
			return "participant";
		default:
			return "";
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			WritableWorkbook wb, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		log.debug("buildExcelDocument() entry");
		
		processServletRequestResponse(req, resp);
        WritableSheet ws = prepareWritableSheet(wb);

        int row = 0, col=0;
        List<RegisterObj> regList = (List<RegisterObj>) req.getSession().getAttribute(BaseController.PARAM_CONTENT_LIST);

        writeColumnHeader(ws, row++, col, new String[]{"Reg Id", "Register Date","Updated Date","Pledger"
        		,"Organisation","Organisation Address","Update Address","Salutation", "First Name", "Last Name", "Designation", "Contact Number"
        		, "Email Address", "Receive Update", "SNEF Member", "SNEF Organisation Name", "Amount", "Status"});
        for(RegisterObj r: regList) {
        	col=0;
        	ws.addCell(new jxl.write.Number(col++, row, r.getId()));
        	if(r.getCreatedDate()==null)
        		col++;
        	else
        		ws.addCell(new DateTime(col++, row, gmtDateTime(r.getCreatedDate())));
        	if(r.getUpdatedDate()==null)
        		col++;
        	else
        		ws.addCell(new DateTime(col++, row, gmtDateTime(r.getUpdatedDate())));
        	ws.addCell(new Label(col++, row, yesNo(r.getPledger())));
        	ws.addCell(new Label(col++, row, StringUtils.trimToEmpty(r.getCompanyName())+StringUtils.trimToEmpty(r.getCompanyNameOther())));
        	ws.addCell(new Label(col++, row, r.getCompanyAddress()));
        	ws.addCell(new Label(col++, row, yesNo(r.isUpdateAddress())));
        	ws.addCell(new Label(col++, row, r.getSalutation()));
        	ws.addCell(new Label(col++, row, r.getFirstName()));
        	ws.addCell(new Label(col++, row, r.getLastName()));
        	ws.addCell(new Label(col++, row, r.getDesignation()));
        	ws.addCell(new Label(col++, row, r.getTelephone()));
        	ws.addCell(new Label(col++, row, r.getEmail()));
        	ws.addCell(new Label(col++, row, yesNo(r.isReceiveUpdate())));
        	ws.addCell(new Label(col++, row, yesNo(r.isSnefMember())));
        	ws.addCell(new Label(col++, row, r.getSnefId()));
        	ws.addCell(new jxl.write.Number(col++, row, r.getAmount()==null?0:r.getAmount(), dollarFormat()));
        	ws.addCell(new Label(col++, row, status(r.getStatus())));
        	row++;
        }
        
        ws = prepareWritableSheet(wb, getTabname(1), 1);
        row = col= 0;
        writeColumnHeader(ws, row++, col, new String[]{"Partipant Id","Pledger"
        		,"Organisation","Salutation", "First Name", "Last Name", "Name to appear on badge", "Designation", "Contact Number"
        		, "Email Address", "Main Conference", "Plenary", "Breakout 1", "Breakout 2", "Post Con 1", "Post Con 2", "Amount", "Status"});
        for(RegisterObj r: regList) {
        	if(r==null||r.getParticipantList()==null||r.getParticipantList().isEmpty())
    			continue;
	        for(ParticipantObj p: r.getParticipantList()) {
	        	col=0;
	        	ws.addCell(new jxl.write.Number(col++, row, p.getId()));
	        	ws.addCell(new Label(col++, row, yesNo(r.getPledger())));
	        	ws.addCell(new Label(col++, row, r.getCompanyName()));
	        	ws.addCell(new Label(col++, row, StringUtils.trimToEmpty(p.getSalutation())));
	        	ws.addCell(new Label(col++, row, p.getFirstName()));
	        	ws.addCell(new Label(col++, row, p.getLastName()));
	        	ws.addCell(new Label(col++, row, p.getBadgeName()));
	        	ws.addCell(new Label(col++, row, p.getDesignation()));
	        	ws.addCell(new Label(col++, row, p.getTelephone()));
	        	ws.addCell(new Label(col++, row, p.getEmail()));
	        	ws.addCell(new Label(col++, row, packageName(p.getPackageId())));
	        	ws.addCell(new Label(col++, row, yesNo(p.isMain())));
	        	ws.addCell(new Label(col++, row, breakout(1, p.getBreakout1())));
	        	ws.addCell(new Label(col++, row, breakout(2, p.getBreakout2())));
	        	ws.addCell(new Label(col++, row, yesNo(p.isPost1())));
	        	ws.addCell(new Label(col++, row, yesNo(p.isPost2())));
	        	ws.addCell(new jxl.write.Number(col++, row, p.getAmount()==null?0:p.getAmount(), dollarFormat()));
	        	ws.addCell(new Label(col++, row, status(p.getStatus())));
	        	row++;
	        }
        }
        
        log.debug("buildExcelDocument() exit");
	}
	
	protected String paymentStatus(int paymentStatus) {
		switch(paymentStatus) {
		case 1:
			return "Completed";
		}
		return "";
	}
	
	protected String packageName(Integer packageId) {
		if(packageId!=null)
		switch(packageId) {
		case 1:
			return "Full Conference";
		case 2:
			return "Plenary Session Only";
		case 3:
			return "Breakout Sessions Only";
		}
		return "";
	}
	
	protected String status(int status) {
		switch(status) {
		case 0:
			return "Pending";
		case 1:
			return "Confirmed";
		case 2:
			return "Cancelled";
		}
		return ""+status;
	}
	
	protected String breakout(int b, Integer breakout) {
		if(breakout==null || breakout<=0)
			return "";
		switch(breakout) {
		case 1:
			return b+"A";
		case 2:
			return b+"B";
		default:
			return breakout+"";				
		}
	}
}
